package model.admin;

import ecom.beans.CategoryBean;
import ecom.beans.EcomAdminRemote;
import ecom.shell.ShellRemote;
import model.AbstractPage;

/**
 *
 * @author Sebastien
 */
public class CategoryManagement extends AbstractPage {

    private CategoryBean category;
    
    private static final String xsl = "C://JAVA//wkspace//ecom//ecom-war//web//XSL/admin/category.xsl";
    private static final String formPage = "admin/category/form.jsp";
    private static final String listPage = "admin/list.jsp";

    public CategoryBean getCategory() {
        return category;
    }

    public void setCategory(CategoryBean category) {
        this.category = category;
    }

    public String addOrUpdate() throws Exception {
        String[] id = (String[]) getParameters().get("id");
        String[] requestAction = (String[]) getParameters().get("action");
        EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");

        if ((id != null && !"0".equalsIgnoreCase(id[0])) || (category != null && category.getIdCateg() != 0)) {
            
            // update
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.updateProductCategory(category.getIdCateg(), category.getName());
            }
            // affichage du formulaire pré-rempli
            else {
                category = admin.getCategoryById(Integer.valueOf(id[0]));
                this.setPage(formPage);
                return SUCCESS;
            }
        } else {

            // insert
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.createProductCategory(category.getName());
            }
            // affichage du formulaire pré-rempli
            else {
                this.setPage(formPage);
                return SUCCESS;
            }
        }
        return list();
    }

    public String list() throws Exception {
        ShellRemote shell = (ShellRemote) getSession().get("shell");
        this.setDataToPrint(shell.executeCommand("category"));
        this.setXslPath(xsl);
        this.setPage(listPage);
        return SUCCESS;
    }

    public String delete() throws Exception {
        String[] id = (String[]) getParameters().get("id");

        if (id != null && !"".equalsIgnoreCase(id[0])) {
            EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");
            admin.removeCategory(Integer.valueOf(id[0]));
        }
        return list();
    }

    @Override
    public void validate() {
        String[] requestAction = (String[]) getParameters().get("action");
        if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
            this.setPage(formPage);

            if (category.getName().length() == 0) {
                this.addFieldError("category.name", "Préciser le nom de la catégorie");
            }
        }
    }

    @Override
    public String execute() throws Exception {
        message = "CategoryManagement()";
        return SUCCESS;
    }
}